package itbour.onetouchshow.activity.video.videoplay;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.xiao.nicevideoplayer.NiceVideoPlayer;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.version1_6.MakeActivityV1_6;
import itbour.onetouchshow.base.BaseIJKPlayActivity;
import itbour.onetouchshow.bean.DesignListBean;
import itbour.onetouchshow.bean.ModelVideoBean;
import itbour.onetouchshow.bean.V1_4DocPreviewBean;
import itbour.onetouchshow.bean.WorkPreviewBean;
import itbour.onetouchshow.bean.user.UserMakeTag;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.platform.WorkPlatform;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.utils.video.MakeProductContrl;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class VideoPlayPreviewActivity extends BaseIJKPlayActivity<VideoplayContract.View, VideoplayPresenter>
        implements VideoplayContract.View {

    @BindView(R.id.id_niceVideoPlayer)
    NiceVideoPlayer niceVideoPlayer;
    @BindView(R.id.activity_detail_player)
    RelativeLayout activityDetailPlayer;
    @BindView(R.id.id_ig_colse)
    ImageView igClose;
    V1_4DocPreviewBean docPreviewBean;
    MakeProductContrl makeProductContrl;
    V1_4DocPreviewBean.PreviewInfoBean.VideoBean video;
    private String docId;
    private String type;
    //private VideoBean videoItem;
    private ModelVideoBean modelVideoBean;
    //直接播放的url不需要通过id拿
    private String playUrl;
    private boolean showBottomController;

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_play_preview;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            docId = extras.getString(AppConst.DOCID);
            playUrl = extras.getString(AppConst.JS_CALLBACK_URL);
            showBottomController = extras.getBoolean(AppConst.KEY_SHOW_BOTTOM_CONTROLLER);
        }
        super.onCreate(savedInstanceState);

        //docId为空 播放url不为空走直接播放的逻辑
        if (TextUtils.isEmpty(docId) && !TextUtils.isEmpty(playUrl)) {
            if (!showBottomController) {
                controller.setShowButtomView(false);
            }
            niceVideoPlayer.releasePlayer();
            niceVideoPlayer.start();
            onChageUrlStartPlay(playUrl, "");
            niceVideoPlayer.seekTo(10);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) niceVideoPlayer.getLayoutParams();
            params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
            params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
            niceVideoPlayer.setLayoutParams(params);

        } else {
            mPresenter.newLoadDocPreview(docId);
        }

        //niceVideoPlayer.setUp(videoUrl, null);
        // onStartPlay(videoUrl, "");
        //mPresenter.loadThisIsCollect(modelVideoBean);
    }

//    @Override
//    protected int videoHeight() {
//        return UIUtils.getHForScan(videoW, VideoH);
//    }

    @Override
    protected int videoHeight() {
        return UIUtils.getHForScan(video.getW(), video.getH());
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        //设置缩略图

        super.afterCreate(savedInstanceState);

        //全屏Activity操作
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

        // mPresenter.loadProductPreviewData(docId);
        igClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected NiceVideoPlayer getmNiceVideoPlayer() {
        return niceVideoPlayer;
    }

    @Override
    public void loadPageInfoSuccess(String result) {
        // modelVideoBean = new Gson().fromJson(result, ModelVideoBean.class);
        //videoItem = modelVideoBean.getVideo();

        // baseInit();
        // niceVideoPlayer.setUp(videoItem.getVideoUrl(), null);
        //initVideoPaly(modelVideoBean.getVideo().getVideoUrl(), modelVideoBean.getName());
    }

    @Override
    public void loadProductPreviewData(String result) {
        WorkPreviewBean videoPreviewBean = new Gson().fromJson(result, WorkPreviewBean.class);
//        videoItem = videoPreviewBean.getVideo();
//        modelVideoBean = new ModelVideoBean();
//        modelVideoBean.setId(videoPreviewBean.getId());
//        modelVideoBean.setVideo(videoItem);

        //  mPresenter.loadThisIsCollect(modelVideoBean);
        //  baseInit();
        //  niceVideoPlayer.setUp(videoPreviewBean.getVideo().getVideoUrl(), null);
        //   initVideoPaly(modelVideoBean.getVideo().getVideoUrl(), modelVideoBean.getName());
    }

    @Override
    public void loadVipInfoSuccess(String result) {

    }

    @Override
    public void loadVipInfoFail(String result) {

    }

    @Override
    public void loaduserDetailSuccess(String result) {

    }

    @Override
    public void loadNewDocPreviewSuccess(String result) {
        dismissProgressDialog();
        docPreviewBean = new Gson().fromJson(result, V1_4DocPreviewBean.class);
        V1_4DocPreviewBean.DocInfoBean docInfo = docPreviewBean.getDocInfo();
        V1_4DocPreviewBean.ProgressInfoBean progressInfo = docPreviewBean.getProgressInfo();
        video = docPreviewBean.getPreviewInfo().getVideo();
        int generateStatus = docInfo.getGenerateStatus();
        MakeProductContrl.instance().setInitialize(new MakeProductContrl.MakeProductListener() {

            @Override
            public void makeSuccess(String result) {
                // makeOverProduct(result);
                niceVideoPlayer.releasePlayer();
                niceVideoPlayer.start();
                onChageUrlStartPlay(video.getVideoUrl(), "");
                niceVideoPlayer.seekTo(10);
            }

            @Override
            public void makeFaile() {
            }

            @Override
            public void makeiing(String result) {

            }

            @Override
            public void lookLater() {
            }

            @Override
            public void isDraft(String result) {

            }
        }, this, MakeProductContrl.SignalTag.SHOW_PROGRESS, UserMakeTag.MAKE)
                .loadNewDocPreviewSuccess(result);
    }


    @Override
    public void loadPreviewBgSuccess(String result) {

    }

    @Override
    public void loadMoudelSuccess(String result) {
        //VideoMoule.VideoModuleBean videoModuleBean = new Gson().fromJson(result, VideoMoule.VideoModuleBean.class);
        Bundle bundle = new Bundle();
        bundle.putString("module", result);
        bundle.putString(WorkPlatform.TYPE, WorkPlatform.getWorkPlatform(WorkPlatform.WOEK));
        openActivity(MakeActivityV1_6.class, bundle);
    }


    @Override
    public void isCollect() {
    }

    @Override
    public void unCollect() {
    }

    @Override
    public void nowFragmentVisable(DesignListBean.ListBean listBean) {

    }

    @Override
    public void collectSuccess(String text) {

    }

    @Override
    public void collectFaild() {

    }


    @Override
    public void finish() {
        super.finish();
        //注释掉activity本身的过渡动画
        overridePendingTransition(0, 0);  //解决退出动画无效
    }
}
